<?php
/*------------------------------------------------------------------------
# com_vportfolio - Vportfolio component
# ------------------------------------------------------------------------
# author    Vsmart Extensions
# copyright Copyright (C) 2010 www.vsmart-extensions.com. All Rights Reserved.
# @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Websites: http://www.vsmart-extensions.com
# Technical Support:  Forum - http://www.vsmart-extensions.com
-------------------------------------------------------------------------*/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.controller' );

/**
 * Portfolio Component Controller
 *
 * @static
 * @package		Joomla
 * @subpackage	Vportfolio
 * @since 1.5
 */
class PortfolioController extends JController
{
	/**
	 * Display the view
	 */
	function display()
	{
		$document =& JFactory::getDocument();

		$viewName	= JRequest::getVar('view', 'category', 'default', 'cmd');
		$viewType	= $document->getType();

		// interceptors to support legacy urls
		switch ($this->getTask())
		{
			case 'category':
				$viewName	= 'category';
				$layout		= 'default';
				break;
			case 'view':				
				$viewName	= 'portfolio';
				$layout		= 'default';
				break;
		}

		// Set the default view name from the Request
		$view = &$this->getView($viewName, $viewType);
		
		// Push a model into the view
		$model	= &$this->getModel( $viewName );
		
		if (!JError::isError( $model )) {
			$view->setModel($model, true); 
		}

		// Workaround for the item view
		if ($viewName == 'portfolio')
		{
			$modelCat	= &$this->getModel( 'category' );
			$view->setModel( $modelCat );
			
		}

		// Display the view
		$view->assign('error', $this->getError());
		$view->display();
	}
	function printFooter(){
		?>
		<!--<div class="powerby">Power by <a href="http://vsmart-extensions.com">Vsmart Extensions</a></div>-->
		<?php
	}
	function saveReview(){
	
		$id = JRequest::getVar('id',0);
		
		$review = JRequest::getVar('review','');
		$customer = JRequest::getVar('customer','');
		$email = JRequest::getVar('email','');
		
		$public = 0;
		
		$modelConfig = &JModel::getInstance( 'vportfolioConfigComponentModel' );
		$config = $modelConfig->getConfigParams();
		
		if($config->cf_allow_review_auto == 1);
			$public = 1;
		
		$db 		=& JFactory::getDBO();
		
		$query = "INSERT INTO #__vportfolio_review SET `review` = '".$review."', `customer`='".$customer."', email = '".$email."',  itemId = '".$id."', `public`='".$public."'";
		$db->setQuery($query);
		$db->query();
		
		if($config->cf_allow_review_auto == 1 && $review != ""){
			echo "<li><p>$review</p><i>- $customer - </i></li>";
		}else{
			echo "Thank you for writing us a review! We will check and public it soon";
		}
		die();
	}
	function rateItem(){
		$id  =  JRequest::getVar('id',0);
		
	   $rate = JRequest::getVar('rate',0);
	   
	   $ip   = $_SERVER['REMOTE_ADDR'];
	   
	   $db 	 =& JFactory::getDBO();
	   
	   //Calculator rate again
	    $query = 'SELECT * FROM #__vportfolio_rating WHERE itemId="'.$id.'" and ip ="'.$ip.'"';
		$db->setQuery($query); 
		$ratings = $db->loadObjectList();
		if(count($ratings) <= 0){
	   
		   $query = "INSERT INTO #__vportfolio_rating SET `rating` = '".$rate."', itemId = '".$id."', ip='".$ip."'";
		   $db->setQuery($query);
		   $db->query();
		   
		}
	   
	   //Calculator rate again
	    $query = 'SELECT * FROM #__vportfolio_rating WHERE itemId="'.$id.'"';
		$db->setQuery($query); 
		$ratings = $db->loadObjectList();
		
		if(count($ratings) <= 0 )
			$rating = 0;
		else{
			$sum = 0;
			$num = count($ratings);
			for($i=0;$i<sizeof($ratings);$i++){
				$sum = $sum + $ratings[$i]->rating;
			}
			$rating = $sum/$num;
			$rating = round($rating);
		}
		//Print
		
		for($k=1;$k<=$rating;$k++){
		    echo "<div class='star_on'>";
			echo "<span class='star'></span>";
			echo "</div>";
		}
		
		for($l=1;$l<=(5-$rating);$l++){
			echo "<div class='star'>";
			echo "<span class='star'></span>";
			echo "</div>";

		}
		die();
			
	}
}